package com.example.demo.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.Jkhomework.JkhomeworkAddRequest;
import com.example.demo.dto.Jkhomework.JkhomeworkQueryRequest;
import com.example.demo.entity.Gkpaper;
import com.example.demo.entity.Jkhomework;
import com.example.demo.exception.CustomException;
import com.example.demo.service.JkhomeworkService;
import com.example.demo.dao.JkhomeworkMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
* @author darling
* @description 针对表【jkhomework】的数据库操作Service实现
*/
@Service
public class JkhomeworkServiceImpl extends ServiceImpl<JkhomeworkMapper, Jkhomework>
    implements JkhomeworkService{

    @Override
    public QueryWrapper<Jkhomework> getQueryWrapper(JkhomeworkQueryRequest jkhomeworkQueryRequest) {
        QueryWrapper<Jkhomework> queryWrapper = new QueryWrapper<>();
        Integer id = jkhomeworkQueryRequest.getId();
        String name = jkhomeworkQueryRequest.getName();
        Integer price = jkhomeworkQueryRequest.getPrice();
        Integer count = jkhomeworkQueryRequest.getCount();
        Integer countprice = jkhomeworkQueryRequest.getCountprice();
        String file = jkhomeworkQueryRequest.getFile();
        String sortField = jkhomeworkQueryRequest.getSortField();
        String sortOrder = jkhomeworkQueryRequest.getSortOrder();
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(price), "price", price);
        queryWrapper.eq(ObjectUtils.isNotEmpty(count), "count", count);
        queryWrapper.eq(ObjectUtils.isNotEmpty(countprice), "countprice", countprice);
        queryWrapper.eq(ObjectUtils.isNotEmpty(file), "file", file);
        // 排序规则
        queryWrapper.orderBy(StrUtil.isNotEmpty(sortField), sortOrder.equals("ascend"), sortField);

        return queryWrapper;
    }

    @Override
    public boolean addjkhomework(MultipartFile file, JkhomeworkAddRequest jkhomeworkAddRequest) {
        if (file.isEmpty()) {
            throw new CustomException("上传文件不能为空");
        }
        // 原始文件名
        String originalFilename = file.getOriginalFilename();
        // 获取文件后缀
        String extname = "";
        int index = originalFilename.lastIndexOf(".");
        if (index > 0) {
            extname = originalFilename.substring(index);
        }
        // 生成新文件名 80cc79ad-9956-4bee-b979-160513143a45.doc
        String newFileName = UUID.randomUUID() + extname;
        // 构建保存路径
        String currentPath = System.getProperty("user.dir");//C:\Users\darling\Desktop\xx\demo
        String saveDir = currentPath + File.separator + "jkhomework";//C:\Users\darling\Desktop\xx\demo\jkhomework
        String fullPath = saveDir + File.separator + newFileName; // C:\Users\darling\Desktop\xx\demo\jkhomework\80cc79ad-9956-4bee-b979-160513143a45.doc
        // 创建目录（如果不存在）
        File dir = new File(saveDir);
        if (!dir.exists()) {
            boolean created = dir.mkdirs();
            if (!created) {
                throw new CustomException("创建目录失败: " + saveDir);
            }
        }
        try {
            // 保存文件
            file.transferTo(new File(fullPath));
        } catch (IOException e) {
            e.printStackTrace();
            throw new CustomException("文件保存失败: " + e.getMessage());
        }
        Jkhomework jkhomework = new Jkhomework();
        BeanUtils.copyProperties(jkhomeworkAddRequest, jkhomework);
        //将newFileName变成/
        jkhomework.setFile("/jkhomework/"+newFileName);
        boolean result = this.save(jkhomework);
        if (!result){
            throw new CustomException("添加失败");
        }
        return true;
    }
}




